//
//  MorePageSubViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/11/7.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "MorePageSubViewController.h"
#import "SinglePageSubViewController.h"
#import "HYTabbarView.h"
#import "SubjectHeadModel.h"
#import "CheckRelustViewController.h"
#import "SubjectNameHeadView.h"
@interface MorePageSubViewController ()

@property (nonatomic,strong) HYTabbarView * tabbarView;

@property(nonatomic,strong) SubjectHeadModel *headModel;

@property(nonatomic,strong)UIButton *LookResultBtn;

@property(nonatomic,strong)UIButton *editBtn;

@property(nonatomic,strong)SubjectNameHeadView *headView;

@end

@implementation MorePageSubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //先请求头部
    
    [self loadHeadData];
 
    if ([self getUserType]==2&&!self.confirmState&&self.CheckLookSelfRecord) {
        
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:self.editBtn];
        
        self.navigationItem.rightBarButtonItem=item;
        
    }
   
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ChangeStatues) name:@"StateChange" object:nil];

    
    // Do any additional setup after loading the view.
}


-(void)ChangeStatues
{
    
    self.confirmState=YES;
    
    self.navigationItem.rightBarButtonItem=nil;
    
}

-(SubjectNameHeadView*)headView
{
    if (!_headView) {
        
        _headView=[[SubjectNameHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
        
        
    }
    return _headView;
}

-(UIButton*)LookResultBtn
{
    if (!_LookResultBtn) {
        
        _LookResultBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        
        if (kDevice_Is_iPhoneX) {
            
            _LookResultBtn.frame=CGRectMake(0, SCREEN_HEIGHT-50-64-IPhoneXTableHeight, SCREEN_WIDTH, 50);
            
        }else{
            
            _LookResultBtn.frame=CGRectMake(0, SCREEN_HEIGHT-50-64, SCREEN_WIDTH, 50);
        }
 
        _LookResultBtn.backgroundColor=TBColorProvideBtnbg;
        
        _LookResultBtn.titleLabel.font=[UIFont systemFontOfSize:18];
        
        [_LookResultBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [_LookResultBtn setTitle:@"查看结果" forState:UIControlStateNormal];
        
        [_LookResultBtn addTarget:self action:@selector(GotoLoookResult:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    
    return _LookResultBtn;
}


-(HYTabbarView*)tabbarView
{
    if (!_tabbarView) {
    
        CGRect frame;
        
        if (kDevice_Is_iPhoneX) {
            
            frame=CGRectMake(0,
                             60,
                             kScreenWidth,
                             kScreenHeight - 64-50-60-IPhoneXTableHeight);
            
        }else{
            
            frame=CGRectMake(0,
                             60,
                             kScreenWidth,
                             kScreenHeight - 64-50-60);
        }
        
        _tabbarView = [[HYTabbarView alloc]initWithFrame:frame];
        for (SubjectHeadData *head in self.headModel.data) {
    
            NewSubjectData *data=[NewSubjectData new];
            
            data.subjectId=self.SubModel.subjectId;
            
             data.resultId=self.SubModel.resultId;
            
             data.viewType=self.SubModel.viewType;
            
             data.typeId=[NSString stringWithFormat:@"%ld",head.typeId];
            
            SinglePageSubViewController *SinglePage=[SinglePageSubViewController new];
            
            SinglePage.title=head.name;
      
            SinglePage.SubModel=data;
            
            SinglePage.ContentUrl=@"/index.php/FoodCheckApi/inspectItemList.json";
            
            SinglePage.OpenLookOther=self.OpenLookOther;
            
            SinglePage.confirmState=self.confirmState;
            
            SinglePage.CheckLookSelfRecord=self.CheckLookSelfRecord;
            
            [_tabbarView addSubItemWithViewController:SinglePage];
            
            SinglePage.Moreblock=^(NSString *SubjectName, NSString *ShopName)
            {

                
                self.headView.SubjectName=SubjectName;
                
            };
            
            [self addChildViewController:SinglePage];
        }
        
    }
    
    return _tabbarView;
}


-(void)loadHeadData
{
    [self showLoadingAnimation];
    
    BaseReqApi *Api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/inspectItemTypeList.json" andrequestTime:5 andParams:@{@"subjectId":[NSString stringWithFormat:@"%ld",self.SubModel.subjectId]} andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [Api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        
        [self stopLoadingAnimation];
        
       self.headModel=[SubjectHeadModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
          
            [self.view addSubview:self.headView];
          
            [self.view addSubview:self.tabbarView];
            
            [self.view addSubview:self.LookResultBtn];
            
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
   
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
            
        }

    }];

}

#pragma mark-查看结果
-(void)GotoLoookResult:(UIButton*)sender
{
    
    if (self.isPop) {
   
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
    
    CheckRelustViewController *checkResult=[CheckRelustViewController new];
    
    checkResult.resultId=self.SubModel.resultId;
    
    [self.navigationController pushViewController:checkResult animated:YES];
    
    }
}

#pragma mark-头部编辑

-(UIButton*)editBtn
{
    if (!_editBtn) {
        
        _editBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _editBtn.frame = CGRectMake(0, 0, 60, 20);
        [_editBtn setTitle:@"编辑" forState:UIControlStateNormal];
        _editBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_editBtn sizeToFit];
        _editBtn.tintColor=[UIColor clearColor];
        
        [_editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        [_editBtn addTarget:self action:@selector(GotoEdit:) forControlEvents:UIControlEventTouchUpInside];

    }
    
    return _editBtn;
}


-(void)GotoEdit:(UIBarButtonItem*)sender
{
   
    
    SinglePageSubViewController *SinglePage=self.childViewControllers[self.tabbarView.selectedIndex];
    
    [SinglePage GotoEdit:nil];
    

    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
